package se392.ateam2006.utils;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.net.URL;
import java.net.URI;
import java.io.File;

/**
 * Read in a set of properties from a file
 * @author Ateam (Matthew Bennett, Claire Melton, Shingai Manyiwa, John Adderley)
 * @version 25/03/07
 */
public class PropertiesManager {
    
    public static final String DEFAULT_CUT_OFF_PERIOD = "DEFAULT_CUT_OFF";
    public static final String WORKING_DAY_START = "WORKING_DAY_START";
    public static final String WORKING_DAY_END = "WORKING_DAY_END";
    public static final String WORKING_DAYS = "WORKING_DAYS";
    
    //Path name for the properties file
    private static final String PATH = "http://users.aber.ac.uk/mwb3/email.properties";
    
    private static Properties properties = null;
    
    /**
     * Load the appropriate property from the properties file
     * @param propertyName the name of the property to return
     * @return String - the value associated with the property key specified
     * @throws java.io.IOException if the properties file cannot be loaded
     */
    public static String getProperty(String propertyName) {
        setupProperties();
        
        return properties.getProperty(propertyName);
    }
    
    /**
     * DO NOT USE A method to the set properties in the properties file.
     * Note: We've removed this method. DO NOT USE.
     * @param propertyName The name of the property to set
     * @param value the new value of the property
     * @throws java.io.IOException if the properties file cannot be loaded
     * @deprecated
     */
    @Deprecated
    public static void setProperty(String propertyName, String value ) {
        
        throw new RuntimeException("Don't be silly" +
                "why do you want to save an email property? \n" +
                "Just edit the file by hand, now there's a good chap.");
    }
    
    /**
     * Set up the email properties
     */
    public static void setupProperties() {
        
        if(null == properties){
            properties = new Properties();
            try {
                
                URL propertiesFile = new URL(PATH);
                System.out.println("Propertiesfile URL: " + propertiesFile.toString());
                BufferedInputStream bi = new BufferedInputStream(propertiesFile.openStream());
                properties.load(bi);
            }
            catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("File could not be loaded " + e.getMessage());
            }
        }else{
            // Don't need to do anything'
        }
    } 
}
